#include "addmodifycommodity.h"
#include "ui_addmodifycommodity.h"

#include "inquiresql.h"
#include "commodity.h"
#include "attributesetting.h"
#include "commodityinformation.h"
#include "enumtypeheader.h"

#include <QDebug>
#include <QKeyEvent>
#include <QMessageBox>

#include <QDesktopWidget>

AddModifyCommodity::AddModifyCommodity(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddModifyCommodity)
{
    ui->setupUi(this);
//    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );
//    this->move((QApplication::desktop()->width() - this->width())/2,
//               (QApplication::desktop()->height() - this->height())/2);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(538, 592);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;

    m_attributeSetting = new AttributeSetting(this);
    m_attributeSetting->hide();

    //正则表达式语句中不能乱加空格
    QRegExp regExpDiscount("[0-9]{1,2}|100");
    QRegExpValidator *pRegExpValidatorDiscount = new QRegExpValidator(regExpDiscount, this);
    ui->lineEditDiscount->setValidator(pRegExpValidatorDiscount);

    QRegExp regExpPrice(" [0-9]{0,6}|[0-9]{0,6}[.]{1}[0-9]{0,2} ");
    QRegExpValidator *pRegExpValidatorPrice = new QRegExpValidator(regExpPrice, this);
    ui->lineEditCost->setValidator(pRegExpValidatorPrice);
    ui->lineEditSellingPrice->setValidator(pRegExpValidatorPrice);
    ui->lineEditMemberPrice->setValidator(pRegExpValidatorPrice);

    QRegExp regExpMinStock("[0-9]{1,5}");
    QRegExpValidator *pRegExpValidatorMinStock = new QRegExpValidator(regExpMinStock, this);
    ui->lineEditMinStock->setValidator(pRegExpValidatorMinStock);

    QRegExp regExpBarcode("[0-9]{1,8}");
    QRegExpValidator *pRegExpValidatorBarcode = new QRegExpValidator(regExpBarcode, this);
    ui->lineEditCommodityBarcode->setValidator(pRegExpValidatorBarcode);
    ui->lineEditInternalCode->setValidator(pRegExpValidatorBarcode);

    //匹配所有合理年月日的正则表达式： ^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$
    QRegExp regExpShelfLife("^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])"
                            "|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]"
                            "|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$");
    QRegExpValidator *pRegExpValidatorShelfLife = new QRegExpValidator(regExpShelfLife, this);
    ui->lineEditShelfLife->setValidator(pRegExpValidatorShelfLife);

    //添加事件过滤器
    ui->lineEditCommodityBarcode->installEventFilter(this);
    ui->lineEditName->installEventFilter(this);
    ui->lineEditInternalCode->installEventFilter(this);
    ui->lineEditCategory->installEventFilter(this);
    ui->comboBoxSpecification->installEventFilter(this);
    ui->comboBoxColor->installEventFilter(this);
    ui->comboBoxBrand->installEventFilter(this);
    ui->lineEditShelfLife->installEventFilter(this);
    ui->comboBoxUnit->installEventFilter(this);
    ui->lineEditCost->installEventFilter(this);
    ui->lineEditDiscount->installEventFilter(this);
    ui->lineEditSellingPrice->installEventFilter(this);
    ui->lineEditMinStock->installEventFilter(this);
    ui->lineEditMemberPrice->installEventFilter(this);
    ui->textEditRemark->installEventFilter(this);
    ui->pushButtonSave->installEventFilter(this);

//    m_attributeSettings = new AttributeSettings;
    m_categorysettings = new CategorySettings(this);
    m_categorysettings->hide();

    connect(m_categorysettings, SIGNAL(displaycategory(QString)),
            this, SLOT(slotDisplayLineEditCategory(QString)));
    connect(m_attributeSetting, SIGNAL(signalUpdateComboBoxAttribute(int)),
            this, SLOT(slotUpdateComboBoxAttribute(int)));

    slotUpdateComboBoxAttribute(SpecificationPage);
    slotUpdateComboBoxAttribute(BrandPage);
    slotUpdateComboBoxAttribute(ColorPage);
    slotUpdateComboBoxAttribute(UnitPage);
//    clearCommodity();
}

AddModifyCommodity::~AddModifyCommodity()
{
    delete ui;
}

bool AddModifyCommodity::eventFilter(QObject *target, QEvent *event)
{
    if (target == ui->lineEditCommodityBarcode || target == ui->lineEditName
            || target == ui->lineEditInternalCode || target == ui->lineEditCategory
            || target == ui->comboBoxSpecification || target == ui->comboBoxColor
            || target == ui->comboBoxBrand || target == ui->lineEditShelfLife
            || target == ui->comboBoxUnit || target == ui->lineEditCost
            || target == ui->lineEditDiscount || target == ui->lineEditSellingPrice
            || target == ui->lineEditMinStock || target == ui->lineEditMemberPrice
            || target == ui->textEditRemark || target == ui->pushButtonSave)
    {        
//        //获得焦点事件
//        if (event->type() == QEvent::FocusIn)
//        {
//        }

        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditCommodityBarcode)
                {
//                    if (m_lastClick != lineEditCommodityBarcode)
//                    {
//                        ui->lineEditCommodityBarcode->selectAll();
                        m_lastClick = lineEditCommodityBarcode;
//                        return true;
//                    }
//                    else if (m_lastClick == lineEditCommodityBarcode)
//                    {
//                        goto flag;
//                    }
//                    ui->lineEditCommodityBarcode->selectAll();
//                    return true;
                }
                if (target == ui->lineEditShelfLife)
                {
                    if (m_lastClick != lineEditShelfLife){
                        m_lastClick = lineEditShelfLife;
                        ui->lineEditShelfLife->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditShelfLife)
                    {
                        goto flag;
                    }
//                    ui->lineEditShelfLife->selectAll();
//                    return true;
                }
                if (target == ui->lineEditName)
                {
                    if (m_lastClick != lineEditName){
                        m_lastClick = lineEditName;
                        ui->lineEditName->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditName)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditInternalCode)
                {
                    if (m_lastClick != lineEditInternalCode){
                        m_lastClick = lineEditInternalCode;
                        ui->lineEditInternalCode->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditInternalCode)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditCategory)
                {
                    m_lastClick = lineEditCategory;
                    ui->lineEditCategory->selectAll();
                    if (m_categorysettings == NULL)
                    {
                        m_categorysettings = new CategorySettings;
                    }
                    QPoint GlobalPoint(ui->lineEditCategory->mapToGlobal(QPoint(0, 0)));
                    int x = GlobalPoint.x();//该控件在窗体中左上角横坐标
                    int y = GlobalPoint.y();//该控件在窗体中左上角纵坐标

//                    m_categorysettings->setWindowFlags(
//                                Qt::ToolTip);
                    m_categorysettings->setWindowFlags(
                                Qt::FramelessWindowHint | Qt::Dialog);
                    m_categorysettings->onlyShowTreeWidget();
                    m_categorysettings->setAttribute(Qt::WA_ShowModal, false);
                    m_categorysettings->setWindowModality(Qt::NonModal);
                    m_categorysettings->move(x, y + 25);
                    m_categorysettings->displayCategory();
                    m_categorysettings->show();
                    return true;
                }
                if (target == ui->lineEditCost)
                {
                    if (m_lastClick != lineEditCost){
                        m_lastClick = lineEditCost;
                        ui->lineEditCost->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditCost)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditDiscount)
                {
                    if (m_lastClick != lineEditDiscount){
                        m_lastClick = lineEditDiscount;
                        ui->lineEditDiscount->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditDiscount)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditSellingPrice)
                {
                    if (m_lastClick != lineEditSellingPrice){
                        m_lastClick = lineEditSellingPrice;
                        ui->lineEditSellingPrice->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditSellingPrice)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditMinStock)
                {
                    if (m_lastClick != lineEditMinStock){
                        m_lastClick = lineEditMinStock;
                        ui->lineEditMinStock->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditMinStock)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditMemberPrice)
                {
                    if (m_lastClick != lineEditMemberPrice){
                        m_lastClick = lineEditMemberPrice;
                        ui->lineEditMemberPrice->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditMemberPrice)
                    {
                        goto flag;
                    }
                }
                if (target == ui->textEditRemark)
                {
                    if (m_lastClick != textEditRemark){
                        m_lastClick = textEditRemark;
                        ui->textEditRemark->selectAll();
                        return true;
                    }else if (m_lastClick == textEditRemark)
                    {
                        goto flag;
                    }
                }
                if (target == ui->comboBoxBrand)
                {
                    ui->comboBoxBrand->setCurrentIndex(-1);
                    return true;
                }
                if (target == ui->comboBoxColor)
                {
                    ui->comboBoxColor->setCurrentIndex(-1);
                    return true;
                }
                if (target == ui->comboBoxSpecification)
                {
                    ui->comboBoxSpecification->setCurrentIndex(-1);
                    return true;
                }
                if (target == ui->comboBoxUnit)
                {
                    ui->comboBoxUnit->setCurrentIndex(-1);
                    return true;
                }
                if (target == ui->lineEditShelfLife)
                {
                    if (m_lastClick != lineEditShelfLife){
                        m_lastClick = lineEditShelfLife;
                        ui->lineEditShelfLife->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditShelfLife)
                    {
                        goto flag;
                    }
                }
            }
        }

        //鼠标左键释放事件
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->comboBoxBrand)
                {
                    ui->comboBoxBrand->showPopup();
                    return true;
                }
                if (target == ui->comboBoxColor)
                {
                    ui->comboBoxColor->showPopup();
                    return true;
                }
                if (target == ui->comboBoxSpecification)
                {
                    ui->comboBoxSpecification->showPopup();
                    return true;
                }
                if (target == ui->comboBoxUnit)
                {
                    ui->comboBoxUnit->showPopup();
                    return true;
                }
            }
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditCommodityBarcode)
                {
                    ui->lineEditName->setFocus();
                    ui->lineEditName->selectAll();
                    return true;
                }
                if (target == ui->lineEditName)
                {
                    ui->lineEditInternalCode->setFocus();
                    ui->lineEditInternalCode->selectAll();
                    return true;
                }
                if (target == ui->lineEditInternalCode)
                {
                    ui->lineEditCategory->setFocus();
                    return true;
                }
                if (target == ui->lineEditCategory)
                {
                    ui->comboBoxSpecification->setFocus();
                    return true;
                }
                if (target == ui->comboBoxSpecification)
                {
                    ui->comboBoxColor->setFocus();
                    return true;
                }
                if (target == ui->comboBoxColor)
                {
                    ui->comboBoxBrand->setFocus();
                    return true;
                }
                if (target == ui->comboBoxBrand)
                {
                    ui->lineEditShelfLife->setFocus();
                    ui->lineEditShelfLife->selectAll();
                    return true;
                }
                if (target == ui->lineEditShelfLife)
                {
                    ui->comboBoxUnit->setFocus();
                    return true;
                }
                if (target == ui->comboBoxUnit)
                {
                    ui->lineEditCost->setFocus();
                    ui->lineEditCost->selectAll();
                    return true;
                }
                if (target == ui->lineEditCost)
                {
                    ui->lineEditDiscount->setFocus();
                    ui->lineEditDiscount->selectAll();
                    return true;
                }
                if (target == ui->lineEditDiscount)
                {
                    ui->lineEditSellingPrice->setFocus();
                    ui->lineEditSellingPrice->selectAll();
                    return true;
                }
                if (target == ui->lineEditSellingPrice)
                {
                    ui->lineEditMinStock->setFocus();
                    ui->lineEditMinStock->selectAll();
                    return true;
                }
                if (target == ui->lineEditMinStock)
                {
                    ui->lineEditMemberPrice->setFocus();
                    ui->lineEditMemberPrice->selectAll();
                    return true;
                }
                if (target == ui->lineEditMemberPrice)
                {
                    ui->textEditRemark->setFocus();
                    ui->textEditRemark->selectAll();
                    return true;
                }
                if (target == ui->textEditRemark)
                {
                    ui->pushButtonSave->setFocus();
                    return true;
                }
                if (target == ui->pushButtonSave)
                {
                    if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
                        on_pushButtonSave_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void AddModifyCommodity::keyPressEvent(QKeyEvent *ev)
{
    switch(ev->key())
    {
        case Qt::Key_Escape:
            this->hide();
            break;
    default:
        QWidget::keyPressEvent(ev);
    }
}

void AddModifyCommodity::displayAddModifyCommodity(int page, int commodityBarcode)
{
    m_list = m_sql->InquireCommodityInformation();
    clearCommodity();

    if (page == addCommodityPage)
    {
        setWindowTitle(tr("添加商品信息"));
        initLineEdit();
        currentPage = addCommodityPage;
        ui->checkBoxAdd->show();
        ui->pushButtonAutoAddCommodityBarcode->setDisabled(false);
        ui->lineEditCommodityBarcode->setDisabled(false);
        ui->lineEditCommodityBarcode->setFocus();
        ui->lineEditCommodityBarcode->selectAll();
    }
    else if(page == modifyCommodityPage)
    {
        setWindowTitle(tr("修改商品信息"));
        currentPage = modifyCommodityPage;
        ui->checkBoxAdd->hide();
        ui->pushButtonAutoAddCommodityBarcode->setDisabled(true);
        ui->lineEditCommodityBarcode->setDisabled(true);

        //显示当前商品信息
        for(int i = 0; i < m_list.count(); ++i)
        {
            if (m_list.at(i).get_commodityBarcode().toInt() == commodityBarcode)
            {
                QString specifications = m_sql->InquireAttributeValue(Specification,
                                                                        m_list.at(i).get_specificationsNumber());
                QString category = m_sql->InquireAttributeValue(Category,
                                                                        m_list.at(i).get_categoryNumber());
                QString color = m_sql->InquireAttributeValue(Color, m_list.at(i).get_colorNumber());
                QString brand = m_sql->InquireAttributeValue(Brand, m_list.at(i).get_brandNumbe());
                QString unit = m_sql->InquireAttributeValue(Unit, m_list.at(i).get_unitNumber());

                ui->lineEditCommodityBarcode->setText(m_list.at(i).get_commodityBarcode());
                ui->lineEditName->setText(m_list.at(i).get_commodityName());
                ui->lineEditInternalCode->setText(m_list.at(i).get_internalCode());
                ui->lineEditCategory->setText(category);
                ui->comboBoxSpecification->setCurrentIndex(
                            comboBoxAttributeItemNumber(SpecificationPage, specifications));
                ui->comboBoxColor->setCurrentIndex(
                            comboBoxAttributeItemNumber(ColorPage, color));
                ui->comboBoxBrand->setCurrentIndex(
                            comboBoxAttributeItemNumber(BrandPage, brand));
                ui->comboBoxUnit->setCurrentIndex(
                            comboBoxAttributeItemNumber(UnitPage, unit));
                ui->lineEditShelfLife->setText(m_list.at(i).get_shelfLife());
                ui->lineEditCost->setText(m_list.at(i).get_cost());
                ui->lineEditDiscount->setText(m_list.at(i).get_discount());
                ui->lineEditSellingPrice->setText(m_list.at(i).get_sellingPrice());
                ui->lineEditMinStock->setText(m_list.at(i).get_minStock());
                ui->lineEditMemberPrice->setText(m_list.at(i).get_memberPrice());
                ui->textEditRemark->setText(m_list.at(i).get_remark());
                break;
            }
        }
        ui->lineEditName->setFocus();
        ui->lineEditName->selectAll();
    }
//    this->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
//    this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
//    this->setAttribute(Qt::WA_ShowModal, true);
//    this->move((QApplication::desktop()->width() - this->width())/2 - 250,
//               (QApplication::desktop()->height() - this->height())/2);
//    this->show();

//    this->setWindowFlags(Qt::Dialog);
//    this->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
//    this->setAttribute(Qt::WA_ShowModal, true);
//    this->move((QApplication::desktop()->width() - this->width())/2,
//               (QApplication::desktop()->height() - this->height())/2);
//    this->show();
}

bool AddModifyCommodity::commodityIsEmpty(QString commodityInformation, QString msg)
{
    if (commodityInformation.isEmpty())
    {
//        QMessageBox msgBox(this);
//        msgBox.setWindowTitle(tr("  ★温馨提示★"));
//        msgBox.setText(msg);
//        msgBox.exec();
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                             "\n"+msg );
        return true;
    }
    return false;
}

void AddModifyCommodity::clearCommodity()
{
    ui->lineEditCommodityBarcode->setText(NULL);
    ui->lineEditName->setText(NULL);
    ui->lineEditInternalCode->setText(NULL);
    ui->lineEditCategory->setText(NULL);
    ui->comboBoxSpecification->setCurrentIndex(-1);
    ui->comboBoxColor->setCurrentIndex(-1);
    ui->comboBoxBrand->setCurrentIndex(-1);
    ui->comboBoxUnit->setCurrentIndex(-1);
    ui->lineEditCost->setText(NULL);
    ui->lineEditDiscount->setText(NULL);
    ui->lineEditSellingPrice->setText(NULL);
    ui->lineEditMinStock->setText(NULL);
    ui->lineEditMemberPrice->setText(NULL);
    ui->textEditRemark->setText(NULL);
    ui->lineEditShelfLife->setText(NULL);
}

int AddModifyCommodity::comboBoxAttributeItemNumber(int attribute, const QString & attributeValue)
{
    QStringList attributeValueList;
    switch (attribute)
    {
        case SpecificationPage:
            attributeValueList = m_sql->inquireAllAttributeValue(SpecificationPage);
            break;
        case BrandPage:
            attributeValueList = m_sql->inquireAllAttributeValue(BrandPage);
            break;
        case ColorPage:
            attributeValueList = m_sql->inquireAllAttributeValue(ColorPage);
            break;
        case UnitPage:
            attributeValueList = m_sql->inquireAllAttributeValue(UnitPage);
            break;
        default:
            break;
    }

    for(int i = 0; i < attributeValueList.count(); ++i)
    {
        if (attributeValue == attributeValueList.at(i))
            return i;
    }

    return 0;
}

void AddModifyCommodity::initLineEdit()
{
    ui->lineEditDiscount->setText("100");
    ui->lineEditMinStock->setText("0");
    ui->lineEditCost->setText("0.00");
    ui->lineEditSellingPrice->setText("0.00");
    ui->lineEditMemberPrice->setText("0.00");
}

void AddModifyCommodity::completionPrice(QLineEdit *lineEdit)
{
    if (lineEdit->text().isEmpty()
            || lineEdit->text().toDouble() == 0)
    {
        lineEdit->setText("0.00");
        return;
    }

    if (lineEdit->text().contains(".", Qt::CaseInsensitive))
    {
        if (lineEdit->text().left(1) == ".")
        {
            lineEdit->setText(lineEdit->text().replace(0, 1, "0."));
            return;
        }
        if (lineEdit->text().right(1) == ".")
        {
            lineEdit->setText(lineEdit->text() + "00");
            return;
        }
        if (lineEdit->text().right(2).contains(".", Qt::CaseInsensitive))
            lineEdit->setText(lineEdit->text() + "0");
    }
    else
    {
        lineEdit->setText(lineEdit->text() + ".00");
    }
}

//void AddModifyCommodity::slot()
//{
//    XClientMessageEvent event;

//    memset( &event, '\0', sizeof( XClientMessageEvent ) );

//    event.type = ClientMessage;
//    Display *display =QX11Info::display();
//    event.message_type = XInternAtom( display, "_NET_ACTIVE_WINDOW", False );
//    event.display = display;
//    event.window = DefaultRootWindow(display);
//    event.format = 32;
//    event.data.l[ 0 ] = 1;
//    event.data.l[ 1 ] = 0;
//    event.data.l[ 2 ] = 0;
//    event.data.l[ 3 ] = 0;
//    event.data.l[ 4 ] = 0;

//    XSendEvent( display, DefaultRootWindow(display), False,
//              SubstructureRedirectMask, (XEvent*)&event );

//}

void AddModifyCommodity::on_pushButtonSave_clicked()
{
    if (currentPage == addCommodityPage)
    {
//        qDebug() << "add";
        if (commodityIsEmpty(ui->lineEditCommodityBarcode->text(), tr("商品编码不能为空")))
            return;
        if (commodityIsEmpty(ui->lineEditName->text(), tr("商品名称不能为空")))
            return;
        if (commodityIsEmpty(ui->lineEditInternalCode->text(), tr("商品内码不能为空")))
            return;
        if (commodityIsEmpty(ui->lineEditCategory->text(), tr("商品类别不能为空")))
            return;
        if (commodityIsEmpty(ui->comboBoxUnit->currentText(), tr("计量单位不能为空")))
            return;

        //向数据库中插入商品信息
        Commodity commodity(ui->lineEditCommodityBarcode->text(),
                            ui->lineEditName->text(),
                            ui->lineEditInternalCode->text(),
                            ui->lineEditCategory->text(),
                            ui->comboBoxSpecification->currentText(),
                            ui->comboBoxColor->currentText(),
                            ui->comboBoxBrand->currentText(),
                            ui->comboBoxUnit->currentText(),
                            ui->lineEditDiscount->text(),
                            ui->lineEditShelfLife->text(),
                            ui->lineEditCost->text(),
                            ui->lineEditSellingPrice->text(),
                            ui->lineEditMemberPrice->text(),
                                    "0",
                            ui->lineEditMinStock->text(),
                            ui->textEditRemark->toPlainText());

//        qDebug() << "Cost" << ui->lineEditCost->text();
//        qDebug() << "SellingPrice" << ui->lineEditSellingPrice->text();
//        qDebug() << "MemberPrice" << ui->lineEditMemberPrice->text();

        if (!(m_sql->InsertCommodityInformation(commodity)))
        {
            QMessageBox::critical(this, tr(" 警告！"), tr("插入商品信息失败") );
            return ;
        }

        if (!ui->checkBoxAdd->isChecked())
            this->hide();
        else
        {
            clearCommodity();
            ui->lineEditCommodityBarcode->setFocus();
        }
    }

    if (currentPage == modifyCommodityPage)
    {
//        qDebug() << "modify";
        //判断是否为空
        if (commodityIsEmpty(ui->lineEditCommodityBarcode->text(), tr("商品编码不能为空")))
            return;
        if (commodityIsEmpty(ui->lineEditName->text(), tr("商品名称不能为空")))
            return;
        if (commodityIsEmpty(ui->lineEditInternalCode->text(), tr("商品内码不能为空")))
            return;
        if (commodityIsEmpty(ui->lineEditCategory->text(), tr("商品类别不能为空")))
            return;
        if (commodityIsEmpty(ui->comboBoxUnit->currentText(), tr("计量单位不能为空")))
            return;
        //修改商品信息
        Commodity commodity(ui->lineEditCommodityBarcode->text(),
                            ui->lineEditName->text(),
                            ui->lineEditInternalCode->text(),
                            ui->lineEditCategory->text(),
                            ui->comboBoxSpecification->currentText(),
                            ui->comboBoxColor->currentText(),
                            ui->comboBoxBrand->currentText(),
                            ui->comboBoxUnit->currentText(),
                            ui->lineEditDiscount->text(),
                            ui->lineEditShelfLife->text(),
                            ui->lineEditCost->text(),
                            ui->lineEditSellingPrice->text(),
                            ui->lineEditMemberPrice->text(),
                                    "0",
                            ui->lineEditMinStock->text(),
                            ui->textEditRemark->toPlainText());

        if (!(m_sql->UpdateCommodityInformation(commodity)))
        {
            QMessageBox::critical(this, tr(" 警告！"), tr("修改商品信息失败") );
            return ;
        }
        this->hide();
    }
    emit displayCommodityInformation();
}

void AddModifyCommodity::on_pushButtonCancel_clicked()
{
    this->hide();
}

void AddModifyCommodity::on_pushButtonAutoAddCommodityBarcode_clicked()
{
    int commodityBarcode = m_list.at(m_list.count() - 1).get_commodityBarcode().toInt() + 1;
    ui->lineEditCommodityBarcode->setText(QString::number(commodityBarcode).rightJustified(8, '0'));
}

void AddModifyCommodity::slotDisplayLineEditCategory(QString category)
{
//    qDebug() << "slotDisplayLineEditCategory";
    ui->lineEditCategory->setText(category);
    m_categorysettings->hide();
//    m_categorysettings->set_lineEditCategoryPress(false);
}

void AddModifyCommodity::slotUpdateComboBoxAttribute(int attribute)
{
    switch (attribute)
    {
        case SpecificationPage:
            ui->comboBoxSpecification->clear();
            ui->comboBoxSpecification->addItems(m_sql->inquireAllAttributeValue(SpecificationPage));
            ui->comboBoxSpecification->setCurrentIndex(-1);
            break;
        case BrandPage:
            ui->comboBoxBrand->clear();
            ui->comboBoxBrand->addItems(m_sql->inquireAllAttributeValue(BrandPage));
            ui->comboBoxBrand->setCurrentIndex(-1);
            break;
        case ColorPage:
            ui->comboBoxColor->clear();
            ui->comboBoxColor->addItems(m_sql->inquireAllAttributeValue(ColorPage));
            ui->comboBoxColor->setCurrentIndex(-1);
            break;
        case UnitPage:
            ui->comboBoxUnit->clear();
            ui->comboBoxUnit->addItems(m_sql->inquireAllAttributeValue(UnitPage));
            ui->comboBoxUnit->setCurrentIndex(-1);
            break;
        default:
            break;
    }

}

void AddModifyCommodity::on_pushButtonAutoAddInternalCode_clicked()
{
    ui->lineEditInternalCode->setText(ui->lineEditCommodityBarcode->text());
}

void AddModifyCommodity::on_lineEditCost_editingFinished()
{
    completionPrice(ui->lineEditCost);
}

void AddModifyCommodity::on_lineEditSellingPrice_editingFinished()
{
    completionPrice(ui->lineEditSellingPrice);
}

void AddModifyCommodity::on_lineEditMemberPrice_editingFinished()
{
    completionPrice(ui->lineEditMemberPrice);
}

void AddModifyCommodity::on_pushButtonAddCategory_clicked()
{   
    m_categorysettings->setWindowFlags(Qt::Dialog);
    m_categorysettings->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_categorysettings->setAttribute(Qt::WA_ShowModal, true);
    m_categorysettings->move((QApplication::desktop()->width() - m_categorysettings->width())/2,
               (QApplication::desktop()->height() - m_categorysettings->height())/2);
//    if (!m_categorysettings->get_lineEditCategoryPress())
        m_categorysettings->showAll();
    m_categorysettings->displayCategory();
    m_categorysettings->show();
}

void AddModifyCommodity::on_pushButtonAddSpecification_clicked()
{
//    emit signalDisplayAttributeSettings(tr("规格设置"));
    m_attributeSetting->displayAttributePage(SpecificationSetting);

    m_attributeSetting->setWindowFlags(Qt::Dialog);
    m_attributeSetting->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_attributeSetting->setAttribute(Qt::WA_ShowModal, true);
    m_attributeSetting->move((QApplication::desktop()->width() - m_attributeSetting->width())/2,
               (QApplication::desktop()->height() - m_attributeSetting->height())/2);
    m_attributeSetting->show();
//    emit signalDisplayAttributeSettings(2);
}

void AddModifyCommodity::on_pushButtonAddColor_clicked()
{
//    emit signalDisplayAttributeSettings(tr("颜色设置"));
    m_attributeSetting->displayAttributePage(ColorSetting);

    m_attributeSetting->setWindowFlags(Qt::Dialog);
    m_attributeSetting->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_attributeSetting->setAttribute(Qt::WA_ShowModal, true);
    m_attributeSetting->move((QApplication::desktop()->width() - m_attributeSetting->width())/2,
               (QApplication::desktop()->height() - m_attributeSetting->height())/2);
    m_attributeSetting->show();
//    emit signalDisplayAttributeSettings(4);
}

void AddModifyCommodity::on_pushButtonAddBrand_clicked()
{
//    emit signalDisplayAttributeSettings(tr("品牌设置"));
    m_attributeSetting->displayAttributePage(BrandsSetting);

    m_attributeSetting->setWindowFlags(Qt::Dialog);
    m_attributeSetting->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_attributeSetting->setAttribute(Qt::WA_ShowModal, true);
    m_attributeSetting->move((QApplication::desktop()->width() - m_attributeSetting->width())/2,
               (QApplication::desktop()->height() - m_attributeSetting->height())/2);
    m_attributeSetting->show();
//    emit signalDisplayAttributeSettings(3);
}

void AddModifyCommodity::on_pushButtonAddUnit_clicked()
{
//    emit signalDisplayAttributeSettings(tr("单位设置"));
    m_attributeSetting->displayAttributePage(UnitSetting);

    m_attributeSetting->setWindowFlags(Qt::Dialog);
    m_attributeSetting->setWindowModality(Qt::ApplicationModal);//在对 QWidget 操作时，不能操作其他东西
    m_attributeSetting->setAttribute(Qt::WA_ShowModal, true);
    m_attributeSetting->move((QApplication::desktop()->width() - m_attributeSetting->width())/2,
               (QApplication::desktop()->height() - m_attributeSetting->height())/2);
    m_attributeSetting->show();
//    emit signalDisplayAttributeSettings(5);
}
